﻿using System.Data;
using Core.Library;
using DataObject;
using DataObject.Ref;

namespace Event.Controls.Album
{
    public partial class ListPhoto : System.Web.UI.UserControl
    {
        #region variables, constants, properties
        private const int _imageWidth = 600;
        private const string DESC = @"<span class=""desc"">{0}</span>";
        protected string[] _linkRound;
        protected int _round;
        protected int _lockVote;
        protected int _displayVote;
        #endregion

        internal bool DataBind(DataTable dt, DataTable infor, int round)
        {
            _lockVote = RefCache.Ref_GetLockVoteStatus() ? 0 : 1;
            _displayVote = RefCache.Ref_GetVoteDisplayStatus() ? 0 : 1;

            if(infor != null && infor.Rows.Count > 0)
            {
                var r = infor.Rows[0];
                _linkRound = new string[3];
                _linkRound[0] = UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID],
                                                          r[Constants.Table_Members.Member_Email],
                                                          (int) MemberRound.Round1);
                _linkRound[1] = UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID],
                                                          r[Constants.Table_Members.Member_Email],
                                                          (int)MemberRound.Round2);
                _linkRound[2] = UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID],
                                                          r[Constants.Table_Members.Member_Email],
                                                          (int)MemberRound.Round3);
                phRound2.Visible = RefCache.Ref_GetCurrentRound() > (int)MemberRound.Round1 && (int)r[Constants.Table_Members.Member_InRound] > (int)MemberRound.Round1;
                phRound3.Visible = RefCache.Ref_GetCurrentRound() > (int)MemberRound.Round2 && (int)r[Constants.Table_Members.Member_InRound] > (int)MemberRound.Round2;
                ltrTitle.Text = r[Constants.Table_Members.Member_AlbumTitle].ToString();
                ltrDesc.Text = r[Constants.Table_Members.Member_AlbumDesc].ToString();
                _round = round;
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];

                    var avatar = Utils.Photo.BuildPhoto(
                        source: r[Constants.Table_Photos.Photo_Url].ToString(),
                        attribute: "desc",
                        attrValue: UnicodeUtility.EnCodeTitle(r[Constants.Table_Photos.Photo_Description]),
                        title: r[Constants.Table_Photos.Photo_Title].ToString(),
                        href: "javascript:void(0);",
                    width: _imageWidth,
                    height: 0,
                    mustThumb: true,
                    w: ConvertUtility.ConvertTo<int>(r[Constants.Table_Photos.Width]),
                    h: ConvertUtility.ConvertTo<int>(r[Constants.Table_Photos.Height]));
                    this.ltrList.Text +=  avatar;

                    if (!string.IsNullOrWhiteSpace(r[Constants.Table_Photos.Photo_Description].ToString()))
                        this.ltrList.Text += string.Format(DESC, r[Constants.Table_Photos.Photo_Description]);
                }
                return true;
            }
            return false;
        }
    }
}